<?php
/**
 * базовый шаблон для вывода jqGrid
 */
foreach ($this->styles as $v) {
    $this->headLink()->appendStylesheet($v);
}

foreach ($this->scripts as $v) {
    $this->headScript()->appendFile($v, 'text/javascript');
}

/*@var $params Arm_Grid_JqGrid_Params */
$params = $this->params;
?>

<table id="<?php echo $params->getIdGrid()?>"></table> 
<div  id="<?php echo $params->getIdPagerPanel()?>"></div>
 <script type="text/javascript">
    function resultManipulition(data)
    {
        var result = data.responseText.split(":");
        if(result[0] != "") {
        if (result[0] == "error" ) {
            alert('Ошибка:' + "\n" + result[1]);
            return false;
         } else {
            //alert(result[1]);
            return true;
         }
        }
    }

    function setError(data)
    {
    	var result = data.responseText.split(":");
        if (result[0] == "error" ) {
             return [false,result[1],""];
         } else {
             return [true,result[1],""];
         }
    }
    
    jQuery(document).ready(function(){
        var lastSel;
        jQuery("#<?php echo $params->getIdGrid()?>").jqGrid({
         <!--  Start Параметры jqGrid -->
          <?php echo $params->getStrsParams()  ?>
           <?php if($params->isInline()) {?>
            ,
            ondblClickRow: function(<?php echo $params->getIdCol()?>) {
                jQuery("#<?php echo $params->getIdGrid()?>").restoreRow(lastSel);
                jQuery("#<?php echo $params->getIdGrid()?>").editRow(<?php echo $params->getIdCol()?>, true, '', resultManipulition);
                lastSel = <?php echo $params->getIdCol()?>;
            },
            onSelectRow: function(<?php echo $params->getIdCol()?>) {
                jQuery("#<?php echo $params->getIdGrid()?>").restoreRow(lastSel);
            }
            <?php }?>
         <!-- End Параметры jqGrid -->    
        });
        
        <?php 
        $atribPager = array();
        foreach ($params->getPagerPanelParams() as $k=>$v) {
            $atribPager[] = $k . ':' . $v;
        }
        ?>
            jQuery("#<?php echo $params->getIdGrid()?>").navGrid('#<?php echo $params->getIdPagerPanel()?>',{<?php echo implode(',', $atribPager)?>}, 
                {
                closeOnEscape:true, 
                afterSubmit: function (data, postdata) {
                var result = data.responseText.split(":");
                   return setError(data);
                }
                }, //  default settings for edit
                {
                    closeOnEscape:true, 
                    afterSubmit: function (data, postdata) {
                	    return setError(data);
                    }
                }, //  default settings for add
                {},  // delete instead that del:false we need this
                {closeOnEscape:true, 
                    afterSubmit: function (data, postdata) {
             	       return setError(data);
                    },
                multipleSearch:true, closeAfterSearch:true}, // search options
                {} /* view parameters*/
            ); 
            // измения размеров grid
            jQuery("#<?php echo $params->getIdGrid()?>").jqGrid('gridResize',{<?php echo $params->getResize()?>});
            // управления столбцами
            jQuery("#<?php echo $params->getIdGrid()?>").jqGrid('navButtonAdd','#<?php echo $params->getIdPagerPanel()?>',{ 
                caption: "<?php echo Arm_Translate::_('Столбцы')?>", 
                title: "<?php echo Arm_Translate::_('Показать скрыть столбцы')?>", 
                onClickButton : function (){ 
                 jQuery("#<?php echo $params->getIdGrid()?>").jqGrid('columnChooser'); 
                }}); 
        }); 
  

    </script>